package a07_二叉树;

import java.util.*;

/**
 * <p>
 * a16_找树左下角的值复习2
 * </p>
 * 最底层，最左边
 *
 * @author flyduck
 * @since 2025/2/13
 */
public class a16_找树左下角的值复习2 {
    public int findBottomLeftValue(TreeNode root) {
        int result = 0;

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()) {
            int originSize = queue.size();
            int size = queue.size();
            while (size > 0){
                TreeNode pollNode = queue.poll();
                if (originSize == size) {
                    result = pollNode.val;
                }
                if(pollNode.left != null){
                    queue.add(pollNode.left);
                }
                if(pollNode.right != null){
                    queue.add(pollNode.right);
                }
                size--;
            }
        }
        return result;
    }
}
